package tpo.lmhprovider.lmhprovider.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dto.GetStock;
import dto.ShowStock;
import dto.ShowStock_list;
import dto.SubStock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.Db_order;
import pojo.Stock_list;
import pojo.Stock_order;
import tpo.lmhprovider.lmhprovider.dao.StockMapper;
import tpo.lmhprovider.lmhprovider.service.StockService;
import until.MyException;
import until.OrderCoderUtil;

import java.sql.SQLException;
import java.util.*;

@Service
public class StockServiceImpl implements StockService {
    @Autowired
    private StockMapper stockMapper;

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public int addStock_order(GetStock getStock) throws SQLException {
        String code= OrderCoderUtil.getByType(getStock.getStock_order().getType());
        Stock_order stock_order=getStock.getStock_order();
        stock_order.setCode(code);
        List<Stock_list> list=getStock.getList();
        if (list!=null){
            for (Stock_list st : list){
                st.setStockCode(code);
                st.setHouseId(stock_order.getHouseId());
            }
            this.addStock_list(list);
        }
        Integer status=stock_order.getStatus();
        Integer type=stock_order.getType();
        if (status==15){
            getStock.getStock_order().setOverDate(new Date());
            switch (type){
                case 8:{// 调拨出库（8）
                    List<SubStock> subList=new ArrayList<>();
                    for (Stock_list sl:list){
                        SubStock subStock=new SubStock();
                        subStock.setStock(sl.getQuantity());
                        subStock.setWarehouseId(stock_order.getHouseId());
                        subStock.setWaresId(sl.getWaresId());
                        subList.add(subStock);
                    }
                    this.subStock(subList);
                    break;
                }
                case 9:{//盘亏出库(9),
                    List<SubStock> subList=new ArrayList<>();
                    for (Stock_list sl:list){
                        SubStock subStock=new SubStock();
                        subStock.setkStock(sl.getQuantity());
                        subStock.setStock(sl.getQuantity());
                        subStock.setWarehouseId(stock_order.getHouseId());
                        subStock.setWaresId(sl.getWaresId());
                        subList.add(subStock);
                    }
                    this.subStock(subList);
                    break;
                }
                case 12:;//增加库存 调拨入库(12), 盘盈入库(13),
                case 13:{
                    List<SubStock> subList=new ArrayList<>();
                    for (Stock_list sl:list){
                        SubStock subStock=new SubStock();
                        subStock.setkStock(sl.getQuantity());
                        subStock.setStock(sl.getQuantity());
                        subStock.setWarehouseId(stock_order.getHouseId());
                        subStock.setWaresId(sl.getWaresId());
                        subList.add(subStock);
                    }
                    this.addStock(subList);
                    break;
                }
            }
        }
        stockMapper.addStock_order(getStock.getStock_order());
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public int addStock_list(List<Stock_list> list) {
        return stockMapper.addStock_list(list);
    }

    @Override
    @Transactional
    public int upStock_order(GetStock getStock) {
        String code=getStock.getStock_order().getCode();
        stockMapper.delStock_list(code);
        stockMapper.delStock_order(code);
        List<Stock_list> list=getStock.getList();
        for (Stock_list st : list){
            st.setStockCode(code);
        }
        if (stockMapper.addStock_order(getStock.getStock_order())>0&&this.addStock_list(getStock.getList())>0)
            return 1;
        return 0;
    }

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public int nextStock_order(Map map) throws SQLException  {
        Integer status=(Integer)map.get("status");
        Integer bStatus=(Integer)map.get("bStatus");
        String code=(String)map.get("code");
        Integer type=(Integer)map.get("type");
        Map map1=new HashMap();
        map1.put("stockCode",code);
        switch (type){
            case 22:;//减少库存 销售出库(7),其他出库(10),采购退货出库(22)
            case 7:;
            case 10:{
                if (status==2&&bStatus==1){
                    List<ShowStock_list> lists=this.getStock_list(map1);
                    List<SubStock> list=new ArrayList<>();
                    for (ShowStock_list sl : lists){
                        SubStock subStock=new SubStock();
                        subStock.setkStock(sl.getQuantity());
                        subStock.setWarehouseId(sl.getWarehouseId());
                        subStock.setWaresId(sl.getWaresId());
                        list.add(subStock);
                    }
                    this.subStock(list);
                }else if (status==4){
                    map.put("overDate",new Date());
                    List<ShowStock_list> lists=this.getStock_list(map1);
                    List<SubStock> list=new ArrayList<>();
                    for (ShowStock_list sl : lists){
                        SubStock subStock=new SubStock();
                        subStock.setStock(sl.getQuantity());
                        subStock.setWarehouseId(sl.getWarehouseId());
                        subStock.setWaresId(sl.getWaresId());
                        list.add(subStock);
                    }
                    this.subStock(list);
                }else if (status==15&&type!=7){
                    map.put("overDate",new Date());
                    List<ShowStock_list> lists=this.getStock_list(map1);
                    List<SubStock> list=new ArrayList<>();
                    for (ShowStock_list sl : lists){
                        SubStock subStock=new SubStock();
                        subStock.setStock(sl.getQuantity());
                        subStock.setWarehouseId(sl.getWarehouseId());
                        subStock.setWaresId(sl.getWaresId());
                        list.add(subStock);
                    }
                    this.subStock(list);
                }else if(status==14){
                    switch (bStatus){
                        default:{
                            List<ShowStock_list> lists=this.getStock_list(map1);
                            List<SubStock> list=new ArrayList<>();
                            for (ShowStock_list sl : lists){
                                SubStock subStock=new SubStock();
                                subStock.setkStock(sl.getQuantity());
                                subStock.setWarehouseId(sl.getWarehouseId());
                                subStock.setWaresId(sl.getWaresId());
                                list.add(subStock);
                            }
                            this.addStock(list);
                            break;
                        }
                        case 4:break;
                        case 15:break;
                        case 1:break;
                    }
                }
                break;
            }
            case 21:;//增加库存 退货入库(11), 其他入库(14),采购入库(21)
            case 11:;
            case 14:{
                if (status==15){
                    map.put("overDate",new Date());
                    List<ShowStock_list> lists=this.getStock_list(map1);
                    List<SubStock> list=new ArrayList<>();
                    for (ShowStock_list sl : lists){
                        SubStock subStock=new SubStock();
                        subStock.setStock(sl.getQuantity());
                        subStock.setkStock(sl.getQuantity());
                        subStock.setWarehouseId(sl.getWarehouseId());
                        subStock.setWaresId(sl.getWaresId());
                        list.add(subStock);
                    }
                    this.addStock(list);
                }
                break;
            }
        }
        return stockMapper.nextStock_order(map);
    }

    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public int subStock(List<SubStock> list) throws SQLException{
        int con=0;
        con=stockMapper.subStock(list);
        return con;
    }

    @Override
    @Transactional
    public int addStock(List<SubStock> list){
        for (SubStock subStock : list){
            if (stockMapper.addStock(subStock)==0){
                stockMapper.addStock2(subStock);
            }
        }
        return 1;
    }

    @Override
    public PageInfo<ShowStock> getStock_order(Map map) {
        PageHelper.startPage((Integer) map.get("pageNum"),(Integer) map.get("pageSize"));
        List list=stockMapper.getStock_order(map);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

    @Override
    public List<ShowStock_list> getStock_list(Map map) {
        return stockMapper.getStock_list(map);
    }

    @Override
    @Transactional
    public int delStock_order(String code) {
        this.delStock_list(code);
        return stockMapper.delStock_order(code);
    }

    @Override
    @Transactional
    public int delStock_list(String stockCode) {
        return stockMapper.delStock_list(stockCode);
    }

    @Override
    public PageInfo<ShowStock> getDb_order(Map map) {
        PageHelper.startPage((Integer) map.get("pageNum"),(Integer) map.get("pageSize"));
        List list=stockMapper.getDb_order(map);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public int addDb_order(GetStock getStock) throws SQLException {
        String code= OrderCoderUtil.getDiaobo(null);
        Db_order db_order=getStock.getDb_order();
        db_order.setSubDate(new Date());
        List<Stock_list> lists=getStock.getList();
        List<SubStock> list=new ArrayList<>();
        db_order.setCode(code);
        for (Stock_list s:lists){
            s.setStockCode(code);
            SubStock subStock=new SubStock();
            subStock.setkStock(s.getQuantity());
            subStock.setWarehouseId(db_order.getChouseId());
            subStock.setWaresId(s.getWaresId());
            list.add(subStock);
        }
        this.addStock_list(lists);
        this.subStock(list);
        return stockMapper.addDb_order(db_order);
    }

    @Override
    public int nextDb_order(Map map) {
        Integer status=(Integer)map.get("status");
        String code=(String)map.get("code");
        if (status==14){
            Integer bStatus=(Integer)map.get("bStatus");
            if (bStatus==6){
                Map map1=new HashMap();
                map1.put("stockCode",code);
                List<ShowStock_list> lists=this.getStock_list(map1);
                List<SubStock> list=new ArrayList<>();
                for (ShowStock_list sl : lists){
                    SubStock subStock=new SubStock();
                    subStock.setkStock(sl.getQuantity());
                    subStock.setWarehouseId(sl.getWarehouseId());
                    subStock.setWaresId(sl.getWaresId());
                    list.add(subStock);
                }
                this.addStock(list);
            }

        }
        return stockMapper.nextDb_order(map);
    }

    @Override
    public PageInfo<Stock_list> getMingXi(Map map) {
        PageHelper.startPage((Integer) map.get("pageNum"),(Integer) map.get("pageSize"));
        List list=stockMapper.getMingXi(map);
        PageInfo pageInfo=new PageInfo(list);
        return pageInfo;
    }
}
